package com.atguigu.flink.chapter07.state;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.FunctionInitializationContext;
import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author lzc
 * @Date 2022/7/8 14:11
 */
public class Flink01_Operator_UnionList {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
    
        env.enableCheckpointing(2000);
    
        env
            .socketTextStream("hadoop162", 9999)
            .flatMap(new MyFlatMap())
            .print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static class MyFlatMap implements FlatMapFunction<String, String>, CheckpointedFunction {
        
        List<String> words = new ArrayList<>();
        private ListState<String> wordsState;
    
        @Override
        public void flatMap(String line, Collector<String> out) throws Exception {
            if (line.contains("x")) {
                throw new RuntimeException("手动抛异常, 让程序自动重启...");
            }
            
            
            String[] data = line.split(" ");
            for (String word : data) {
                words.add(word);
                
            }
            out.collect(words.toString());
        }
        
        // 做快照
        // 周期的执行.
        @Override
        public void snapshotState(FunctionSnapshotContext context) throws Exception {
            // 可以把ArrayLis中的数据存入到状态中
//            System.out.println("MyFlatMap.snapshotState");

//            wordsState.addAll(words);
            wordsState.update(words);
            
            
        }
        
        // 初始状态
        @Override
        public void initializeState(FunctionInitializationContext ctx) throws Exception {
            // 当程序启动的时候执行. 可以从状态中恢复数据
            // 执行的次数和并行度相关
            System.out.println("MyFlatMap.initializeState");
    
            wordsState = ctx.getOperatorStateStore().getUnionListState(new ListStateDescriptor<String>("wordsState", String.class));
            
            // 把列表状态中的值取出, 存储到ArrayList中
            Iterable<String> it = wordsState.get();
            for (String word : it) {
    
                words.add(word);
            }
    
    
        }
    }
    
}
